<?php

// TTTTTTTTTTTTTTTTTTTTTTT                  MMMMMMMM               MMMMMMMM                                CCCCCCCCCCCCMMMMMMMM               MMMMMMMM  SSSSSSSSSSSSSSS 
// T:::::::::::::::::::::T                  M:::::::M             M:::::::M                             CCC::::::::::::M:::::::M             M:::::::MSS:::::::::::::::S
// T:::::::::::::::::::::T                  M::::::::M           M::::::::M                           CC:::::::::::::::M::::::::M           M::::::::S:::::SSSSSS::::::S
// T:::::TT:::::::TT:::::T                  M:::::::::M         M:::::::::M                          C:::::CCCCCCCC::::M:::::::::M         M:::::::::S:::::S     SSSSSSS
// TTTTTT  T:::::T  TTTTTppppp   ppppppppp  M::::::::::M       M::::::::::M   eeeeeeeeeeee          C:::::C       CCCCCM::::::::::M       M::::::::::S:::::S            
//         T:::::T       p::::ppp:::::::::p M:::::::::::M     M:::::::::::M ee::::::::::::ee       C:::::C             M:::::::::::M     M:::::::::::S:::::S            
//         T:::::T       p:::::::::::::::::pM:::::::M::::M   M::::M:::::::Me::::::eeeee:::::ee     C:::::C             M:::::::M::::M   M::::M:::::::MS::::SSSS         
//         T:::::T       pp::::::ppppp::::::M::::::M M::::M M::::M M::::::e::::::e     e:::::e     C:::::C             M::::::M M::::M M::::M M::::::M SS::::::SSSSS    
//         T:::::T        p:::::p     p:::::M::::::M  M::::M::::M  M::::::e:::::::eeeee::::::e     C:::::C             M::::::M  M::::M::::M  M::::::M   SSS::::::::SS  
//         T:::::T        p:::::p     p:::::M::::::M   M:::::::M   M::::::e:::::::::::::::::e      C:::::C             M::::::M   M:::::::M   M::::::M      SSSSSS::::S 
//         T:::::T        p:::::p     p:::::M::::::M    M:::::M    M::::::e::::::eeeeeeeeeee       C:::::C             M::::::M    M:::::M    M::::::M           S:::::S
//         T:::::T        p:::::p    p::::::M::::::M     MMMMM     M::::::e:::::::e                 C:::::C       CCCCCM::::::M     MMMMM     M::::::M           S:::::S
//       TT:::::::TT      p:::::ppppp:::::::M::::::M               M::::::e::::::::e                 C:::::CCCCCCCC::::M::::::M               M::::::SSSSSSS     S:::::S
//       T:::::::::T      p::::::::::::::::pM::::::M               M::::::Me::::::::eeeeeeee          CC:::::::::::::::M::::::M               M::::::S::::::SSSSSS:::::S
//       T:::::::::T      p::::::::::::::pp M::::::M               M::::::M ee:::::::::::::e            CCC::::::::::::M::::::M               M::::::S:::::::::::::::SS 
//       TTTTTTTTTTT      p::::::pppppppp   MMMMMMMM               MMMMMMMM   eeeeeeeeeeeeee               CCCCCCCCCCCCMMMMMMMM               MMMMMMMMSSSSSSSSSSSSSSS   
//                        p:::::p                                                                                                                                       
//                        p:::::p                                                                                                                                       
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       p:::::::p                                                                                                                                      
//                       ppppppppp                                                                                                                                      

//  _____      __  __         ____ __  __ ____  
// |_   __ __ |  \/  | ___   / ___|  \/  / ___|     | AUTHOR: Xiaohe
//   | || '_ \| |\/| |/ _ \ | |   | |\/| \___ \     | EMAIL: 496631085@qq.com
//   | || |_) | |  | |  __/ | |___| |  | |___) |    | WECHAT: he4966
//   |_|| .__/|_|  |_|\___|  \____|_|  |_|____/     | DATETIME: 2023/03/22
//      |_|                                         | TpMeCMS

declare(strict_types=1);

namespace ImiApp\ApiServer\Api\Controller\user;

use Imi\Db\Db;
use Imi\Server\Http\Route\Annotation\Action;
use Imi\Server\Http\Route\Annotation\Controller;
use Imi\Server\Http\Route\Annotation\Route;
use Psr\Http\Message\ResponseInterface;
use Imi\Aop\Annotation\Inject;
// use Phpben\Imi\Validate\Annotation\Validate;
use Phpben\Imi\Auth\Annotation\Auth;


/**
 * 用户我的
 * @Auth(name="token",nologin={"no"})
 * @Controller("/user.me/")
 */
class Me extends UserBean
{
    protected $noNeedLogin = ['no'];
    protected $noNeedRight = ['*'];

    /**
     * 用户获取我的app列表
     *
     * @return void
     * @Action
     */
    public function my_app()
    {

        return $this->success('', $this->GetUserSetmod($this->auth->user()->id, $this->auth->user()->set_id));
    }

    /**
     * 获取提现服务费
     *
     * @Action
     */
    public function get_money_out_ratio()
    {
        //   * @param int $service_ratio_id 服务比例id1=支付现金贝壳返现%,2=积分最多可用%,3=积分服务费%,4=拍卖服务费%,5=立即买单 限额/日(元),6=提现服务费%,7=贝壳转移服务费%
        $ratio = $this->GetSetserveruserVal($this->auth->user()->id, $this->auth->user()->set_id, 6);
        return $this->success('提现服务费', $ratio);
    }

    /**
     * 获取转移贝壳服务费
     *
     * @Action
     */
    public function get_transfer_shell_ratio()
    {
        //   * @param int $service_ratio_id 服务比例id1=支付现金贝壳返现%,2=积分最多可用%,3=积分服务费%,4=拍卖服务费%,5=立即买单 限额/日(元),6=提现服务费%,7=贝壳转移服务费%
        $ratio = $this->GetSetserveruserVal($this->auth->user()->id, $this->auth->user()->set_id, 7);
        return $this->success('贝壳转移服务费', $ratio);
    }

    /**
     * 转移贝壳
     *
     * @param float $shell 贝壳
     * @param string $pay_password 支付密码
     * @return void
     * @Action
     */
    public function transfer_shell(?float $shell = null, $pay_password = null)
    {
        //验证密码 
        $password = $this->VerifyUserPayPassword($this->auth->user()->id, $pay_password);
        if ($password != 'ok') {
            return $this->error('支付密码错误', null, CODE_用户支付密码错误);
        }
        if ($this->auth->shell() < $shell) {
            return $this->error('贝壳不足');
        }
        $ratio = $this->GetSetserveruserVal($this->auth->user()->id, $this->auth->user()->set_id, 7);
        $ratio = ((float)$ratio / 100);
        $服务费 = round($ratio * $shell, 2);
        $到账 = $shell - $服务费;
        $this->IncUserMoney($this->auth->user()->id, $到账, $shell . '贝壳转余额', 35, 0);
        $this->DecUsershell($this->auth->user()->id, $shell, '贝壳转余额', 35, 0);
        return $this->success('转移成功');
    }






    /**
     * 获取保证金变动列表🐲
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @param string $date 月
     * @param string $status 1=支出,2=收入
     * @param string $type_status 类型:act_surety=活动保证金,surety=保证金
     * @return void
     * @Action
     */
    public function get_surety_log_list(?string $date = null, ?int $page = 1, ?int $limit = 10, ?string $status = null,  ?string $type_status = null)
    {


        $page = $page ?: 1;
        $limit = $limit ?: 10;
        $data['user_id'] = $this->auth->user()->id;

        if ($date) {
            list($s, $e) = $this->GetMonthTime($date);
            $data['createtime'] = ['>=', $s];
            $data['and'] = ['createtime' => ['<=', $e]];
        }
        if ($type_status)
            $data['type_status'] = $type_status;

        $inc = Db::name('user_surety_log')->whereEx($data)->where('status', '=', '2')->sum('surety');
        $dec = Db::name('user_surety_log')->whereEx($data)->where('status', '=', '1')->sum('surety');
        if (!$inc)
            $inc = 0;
        if (!$dec)
            $dec = 0;

        if ($status !== null && $status !== '')
            $data['status'] = $status;

        $list = Db::name('user_surety_log')->whereEx($data)->orderRaw('id desc')->page($page, $limit)->select()->getArray();
        foreach ($list as &$val) {
            $val['date'] = date('Y-m-d H:i:s', $val['createtime']);
        }
        $freeze = Db::name('freeze')
            ->where('user_id', '=', $this->auth->user()->id)
            ->where('user_status', '=', 'user')
            ->where('pay_type', '=', 'surety')
            ->where('pay_status', '=', '2') //支付状态:2=已支付,3=已解冻,4=已超时
            ->sum('surety');
        $this->success('ok', ['inc' => $inc, 'dec' => $dec, 'list' => $list, 'freeze' => $freeze ? $freeze : 0]);
    }


    

    /**
     * 退活动保证金
     *
     * @param string $pay_password 支付密码
     * @param string $money 退金额
     * @return void
     * @Action
     */
    public function refund_user_surety( ?string $pay_password = null, ?float $money = 0)
    {
        $user = Db::name('user')->where('id','=',$this->auth->user()->id)->find();
        if($user['surety']<$money){
            return $this->error('保证金不足');
        }
        $this->DecUserSurety($this->auth->user()->id,  $money, '退保证金', '退保证金', 0, 'surety');
        $this->IncUserMoney($this->auth->user()->id,$money,'退保证金', CODE_用户退保证金, 0);//52
        return $this->success('退保证金成功');
    }



    /**
     * 支付店铺保证金或活动保证金
     *
     * @param integer $pay_type 支付类型:1=微信,2=支付宝,3=钱包,9=其他
     * @param string $pay_password 支付密码
     * @param string $money 充值金额
     * @return void
     * @Action
     */
    public function pay_user_surety_order($pay_type = 3, ?string $pay_password = null, ?float $money = 0)
    {

        switch ($pay_type) {
            case '3':
                //验证密码 
                // $this->VerifyStorePayPassword($this->store_id,$pay_password);
                $res = $this->VerifyUserPayPassword($this->auth->user()->id, $pay_password);
                if ($res != 'ok') {
                    return $this->error('支付密码错误', null, CODE_用户支付密码错误);
                }
                break;

            default:
                break;
        }

        if ($money < 0.01) {
            return $this->error('请输入支付金额');
        }


        //这里只是金额

        $user_surety_order = Db::name('user_surety_order')
        // ->whereEx($where)
        ->where('money','=',$money)
        ->where('user_id', '=', $this->auth->user()->id)->where('pay_status', '=', '1')->select()->get(); //支付状态:1=待支付,2=已支付,3=已退款,4=已超时

        //创建支付订单
        if ($user_surety_order) {
            //
            $this->PayUserSuretyorder($user_surety_order, $pay_type);
        } else {
            //没有就创建订单

           
            $in_data['user_id'] = $this->auth->user()->id;
            $in_data['pay_status'] = '1'; //支付状态:1=待支付,2=已支付,3=已退款,4=已超时
            // $res = Db::name('store_caorder')->whereEx($in_data)->select()->get();

            // if (!$res) {
            $in_data['money'] = $money;
            $in_data['price'] = $money;
            $in_data['num'] = Db::name('user_surety_order')
            // ->whereEx($where)
            ->where('user_id', '=', $this->auth->user()->id)->where('pay_status', '=', '2')->count() + 1;

            $in_data['order'] = $this->rand_order('Surety', 28);
            $in_data['time'] = time();
  

            $in_data['pay_type']  = $pay_type;
            $in_data['id'] = Db::name('user_surety_order')->insert($in_data)->getLastInsertId();
            // }
            $this->PayUserSuretyorder($in_data, $pay_type);
            // return $this->error('没有认证订单');
        }
    }






    /**
     * 获取我的积分记录列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @param string $date 月
     * @param string $status 1=支出,2=收入
     * @param int $typestatus_id 类型
     * @return void
     * @Action
     */
    public function get_score_log_list(?string $date = null, ?int $page = 1, ?int $limit = 10, ?string $status = null, ?int  $typestatus_id = null)
    {
        $page = $page ?: 1;
        $limit = $limit ?: 10;
        $data = [];
        $data['user_id'] = $this->auth->user()->id;

        if ($date) {
            list($s, $e) = $this->GetMonthTime($date);
            $data['createtime'] = ['>=', $s];
            $data['and'] = ['createtime' => ['<=', $e]];
        }

        $inc = Db::name('user_score_log')->whereEx($data)->where('status', '=', '2')->sum('score');
        $dec = Db::name('user_score_log')->whereEx($data)->where('status', '=', '1')->sum('score');
        if (!$inc)
            $inc = 0;
        if (!$dec)
            $dec = 0;

        if ($status !== null && $status !== '')
            $data['status'] = $status;
        if ($typestatus_id)
            $data['typestatus_id'] = $typestatus_id;
        $list = Db::name('user_score_log')->whereEx($data)->orderRaw('id desc')->page($page, $limit)->select()->getArray();
        foreach ($list as &$val) {
            $val['date'] = date('Y-m-d H:i:s', $val['createtime']);
        }
        $freeze = Db::name('freeze')
            ->where('user_id', '=', $this->auth->user()->id)
            ->where('user_status', '=', 'user')
            ->where('pay_type', '=', 'score')
            ->where('pay_status', '=', '2') //支付状态:2=已支付,3=已解冻,4=已超时
            ->sum('score');
        $this->success('ok', ['inc' => $inc, 'dec' => $dec, 'list' => $list, 'freeze' => $freeze ? $freeze : 0]);
    }


    /**
     * 获取我的钱包记录列表
     *
     * @param int $page 1
     * @param string $date 月
     * @param int $limit 10
     * @param string $status 1=支出,2=收入
     * @param int $typestatus_id 类型13退款,31提现
     * @return void
     * @Action
     */
    public function get_money_log_list(?string $date = null, ?int $page = 1, ?int $limit = 10, ?string $status = null, ?int  $typestatus_id = null)
    {
        $page = $page ?: 1;
        $limit = $limit ?: 10;
        $data = [];
        $data['user_id'] = $this->auth->user()->id;

        if ($date) {
            list($s, $e) = $this->GetMonthTime($date);
            $data['createtime'] = ['>=', $s];
            $data['and'] = ['createtime' => ['<=', $e]];
        }

        $inc = Db::name('user_money_log')->whereEx($data)->where('status', '=', '2')->sum('money');
        $dec = Db::name('user_money_log')->whereEx($data)->where('status', '=', '1')->sum('money');
        if (!$inc)
            $inc = 0;
        if (!$dec)
            $dec = 0;

        if ($status !== null && $status !== '')
            $data['status'] = $status;
        if ($typestatus_id)
            $data['typestatus_id'] = $typestatus_id;

        $list = Db::name('user_money_log')->whereEx($data)->orderRaw('id desc')->page($page, $limit)->select()->getArray();
        foreach ($list as &$val) {
            $val['date'] = date('Y-m-d H:i:s', $val['createtime']);
        }

        $this->success('ok', ['inc' => $inc, 'dec' => $dec, 'list' => $list]);
    }

    /**
     * 获取我的贝壳记录列表
     *
     * @param integer $page 1
     * @param integer $limit 10
     * @param string $date 月
     * @param string $status 1=支出,2=收入
     * @param int $typestatus_id 
     * @return void
     * @Action
     */
    public function get_shell_log_list(?string $date = null, ?int $page = 1, ?int $limit = 10, ?string $status = null, ?int  $typestatus_id = null)
    {


        $page = $page ?: 1;
        $limit = $limit ?: 10;
        $data = [];
        $data['user_id'] = $this->auth->user()->id;

        if ($date) {
            list($s, $e) = $this->GetMonthTime($date);
            $data['createtime'] = ['>=', $s];
            $data['and'] = ['createtime' => ['<=', $e]];
        }

        $inc = Db::name('user_shell_log')->whereEx($data)->where('status', '=', '2')->sum('shell');
        $dec = Db::name('user_shell_log')->whereEx($data)->where('status', '=', '1')->sum('shell');
        if (!$inc)
            $inc = 0;
        if (!$dec)
            $dec = 0;

        if ($status !== null && $status !== '')
            $data['status'] = $status;
        if ($typestatus_id)
            $data['typestatus_id'] = $typestatus_id;
        $list = Db::name('user_shell_log')->whereEx($data)->orderRaw('id desc')->page($page, $limit)->select()->getArray();
        foreach ($list as &$val) {
            $val['date'] = date('Y-m-d H:i:s', $val['createtime']);
        }
        $transfer = Db::name('user_shell_log')->where('user_id', '=', $this->auth->user()->id)->where('typestatus_id', '=', 35)->sum('shell');
        $freeze = Db::name('freeze')
            ->where('user_id', '=', $this->auth->user()->id)
            ->where('user_status', '=', 'user')
            ->where('pay_type', '=', 'shell')
            ->where('pay_status', '=', '2') //支付状态:2=已支付,3=已解冻,4=已超时
            ->sum('shell');

        $this->success('ok', ['inc' => $inc, 'dec' => $dec, 'list' => $list, 'freeze' => $freeze ? $freeze : 0, 'transfer' => $transfer ? $transfer : 0]);


    

        // $page = $page ?: 1;
        // $limit = $limit ?: 10;
        // $data = [];
        // if ($status !== null && $status !== '')
        //     $data['status'] = $status;
        // if ($typestatus_id)
        //     $data['typestatus_id'] = $typestatus_id;
        // $list = Db::name('user_shell_log')->whereEx($data)->where('user_id', '=', $this->auth->user()->id)->orderRaw('id desc')->page($page, $limit)->select()->getArray();
        // foreach ($list as &$val) {
        //     $val['date'] = date('Y-m-d H:i:s', $val['createtime']);
        // }
        // $this->success('ok', $list);
    }

    /**
     * 用户升级
     *
     * @return void
     * @Action
     */
    public function user_upgrade(){

        $data = $this->UserSetIsRiseTeamNum2($this->auth->user()->id);
        if($data['code']>0){
            return $this->success($data['msg']);
        }else{
            return $this->error($data['msg']);
        }
    }
}
